package cn.chenxiejia;

import java.lang.reflect.InvocationTargetException;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: R1
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/2/9 16:31
 */
public class R1 {
    public static void main(String[] args) {
        try {
            //User user=new User()这里的user用于获取到方法 运行时对象
            var user = Class.forName("cn.chenxiejia.User");
            //user.setName("张三");
            var setage = user.getMethod("setAge", int.class);
            var setid = user.getMethod("setId", int.class);
            var setname = user.getMethod("setName", String.class);//获取setName方法
            //new User();实例化一个对象
            var co = user.getConstructor();
            var o = co.newInstance();
            //传入参数
            setage.invoke(o, 18);
            setname.invoke(o, "张三");
            setid.invoke(o, 1);
            System.out.println(o);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}
